<template>
	<view class="app" v-show="show" :style="skin">
		<u-popup ref="popup" v-model="show" mode="bottom" border-radius="30" closeable>
			<view class="uni-block-modal uni-flex-column cart_div">
				<view class="head uni-flex-row">
					<vimage class="img_box" v-if="selectimg||details.master" :src="selectimg ? selectimg : details.master"></vimage>
					<view class="detail uni-flex-column">
						<text class="name u-line-2">{{ details.name }}</text>
						<block v-if="!has_group && !has_seckill && !has_presell">
							<text class="price" v-if="trolley.type == 2">{{ priceSum }}积分</text>
							<text class="price u-margin-top-10" v-bind:class="is_vip ? 'vip-price' : ''" v-else>{{ is_vip ? '会员价:' : '' }}&yen;&nbsp;{{ totalprice }}</text>
						</block>
						<text v-if="has_group" class="price u-margin-top-10">
							拼团价:&yen;&nbsp;{{ totalprice }}
							<block v-if="!g_id">{{ Number(selectsku.leader_price) > -1 || has_leader ? '(团长价)' : '' }}</block>
						</text>
						<text v-if="has_seckill" class="price u-margin-top-10">秒杀价:&yen;&nbsp;{{ totalprice }}</text>
						<text v-if="has_presell" class="price u-margin-top-10">预售价:&yen;&nbsp;{{ totalprice }}</text>
					</view>
				</view>

				<scroll-view scroll-y="true" class="trolly_div">
					<block v-if="skuTypeList.length">
						<view class="color uni-flex-column uni-underline" v-for="(skuitem, i) in skuTypeList" :key="i">
							<text class="title">{{ skuitem.name }}选择</text>
							<view class="list uni-flex-row">
								<block v-for="(option, c) in skuitem.attrValue" :key="c">
									<text
										:class="[option.disabled && g_id ? 'disabled' : '', option.checked ? 'active' : '', 'option', 'uni-center', 'sm-font']"
										@click="getSku(option, skuitem)"
									>
										{{ option.value }}
									</text>
								</block>
							</view>
						</view>
					</block>
					<block v-if="details.has_attach">
						<view class="color uni-flex-column uni-underline" v-for="(attachitem, e) in attachlist" :key="attachitem.id">
							<text class="title">{{ attachitem.name }}</text>
							<view class="list uni-flex-row">
								<block v-for="(option, o) in attachitem.list" :key="o">
									<!-- 	<view @click="getattach(o, e)" :class="[option.checked ? 'active' : '', 'option', 'uni-center', 'sm-font']">
										<text>{{ option.name }}</text>
										<text class="option-line"></text>
										<text>￥{{ option.price }}</text>
									</view> -->
									<view
										@click="getattach(o, e)"
										:class="[option.total == 0 ? 'disabled' : '', option.checked ? 'active' : '', 'option', 'uni-center', 'sm-font']"
									>
										<text>{{ option.name }}</text>

										<block v-if="option.price > 0">
											<text class="option-line"></text>
											<text>￥{{ option.price }}</text>
										</block>
										<block v-if="option.total == 0">
											<text class="option-line"></text>
											<text>暂无库存</text>
										</block>
									</view>
								</block>
							</view>
						</view>
					</block>
					<view class="color uni-flex-column uni-underline" v-if="!g_id && selectsku.has_group">
						<text class="title">成团选择</text>
						<view class="list uni-flex-row">
							<block v-for="(option, o) in success_num" :key="o">
								<view @click="selectgroup(o)" :class="[group_level == o ? 'active' : '', 'option', 'uni-center', 'sm-font']">
									<text>{{ option }}人团</text>
									<text class="option-line"></text>
									<text>￥{{ selectsku.activity_price[o] }}</text>
								</view>
							</block>
						</view>
					</view>
					<view class="color uni-flex-column uni-underline" v-if="!g_id && details.activity_prices && has_group">
						<text class="title">成团选择</text>
						<view class="list uni-flex-row">
							<block v-for="(option, o) in success_num" :key="o">
								<view @click="selectgroup(o)" :class="[group_level == o ? 'active' : '', 'option', 'uni-center', 'sm-font']">
									<text>{{ option }}人团</text>
									<text class="option-line"></text>
									<text>￥{{ details.activity_prices[o] }}</text>
								</view>
							</block>
						</view>
					</view>
				</scroll-view>

				<view class="num uni-flex-row uni-flex-title uni-underline">
					<view>
						<text>购买数量</text>

						<text class="tip sm-font" v-if="!details.is_limitation">
							<template v-if="details.inventory_type != 2">
								库存{{ min_quantity }}
							</template>
						</text>
						<text class="tip sm-font" v-else>
							<template v-if="details.inventory_type != 2">
								库存{{ min_quantity }}
							</template>
							,（限购{{ details.limitation }}件）
						</text>
					</view>
					<u-number-box disabled-input :min="1" :max="details.is_limitation ? details.limitation : min_quantity" v-model="count"></u-number-box>
				</view>

				<block v-if="!has_group && !has_seckill && !has_presell">
					<view class="button" v-if="trolley.type != 2 && trolley.type != 3">
						<text :class="[hasBuy && is_attch_buy && min_quantity > 0 ? 'uni-background-button1' : 'disabled uni-button', 'uni-button ']" @click="confirm(1)">
							加入购物车
						</text>
						<text :class="[hasBuy && is_attch_buy && min_quantity > 0 ? 'uni-background-button' : 'disabled uni-button buy', 'uni-button buy ']" @click="confirm(2)">
							立即购买
						</text>
					</view>
					<view class="button" v-else>
						<text
							v-if="trolley.type == 2"
							:class="[hasBuy ? 'uni-background-button' : 'disabled uni-button uni-button-buy ', 'uni-button uni-button-buy  ']"
							@click="confirm(3)"
						>
							立即兑换
						</text>
						<text
							v-if="trolley.type == 3"
							:class="[hasBuy ? 'uni-background-button' : 'disabled uni-button uni-button-buy ', 'uni-button uni-button-buy  ']"
							@click="confirm(3)"
						>
							确定
						</text>
					</view>
				</block>
				<block v-if="has_group && details.group">
					<view class="button" v-if="!g_id">
						<text
							v-if="details.group.use_single == 1"
							:class="[hasBuy && is_attch_buy && min_quantity > 0 ? 'uni-background-button1' : 'disabled uni-button', 'uni-button ']"
							@click="confirm(6)"
						>
							单买(￥{{ singleprice }})
						</text>
						<text
							:class="[
								hasBuy && is_attch_buy && min_quantity > 0 && details.group.status == 1 ? 'uni-background-button' : 'disabled uni-button buy',
								details.group.use_single == 0 ? 'group' : '',
								'uni-button buy '
							]"
							@click="confirm(5)"
						>
							{{ details.group.status == 1 ? '拼团' : '即将开始' }}
						</text>
					</view>
					<view class="button" v-else>
						<text
							:class="[
								hasBuy && is_attch_buy && min_quantity > 0 && details.group && details.group.status == 1 ? 'uni-background-button' : 'disabled uni-button group',
								'uni-button group '
							]"
							@click="confirm(5)"
						>
							{{ details.group.status == 1 ? '立即拼团' : '即将开始' }}
						</text>
					</view>
				</block>
				<block v-if="has_seckill">
					<view class="button">
						<text
							v-if="details.seckill.use_single == 1"
							:class="[hasBuy && is_attch_buy && min_quantity > 0 ? 'uni-background-button1' : 'disabled uni-button', 'uni-button ']"
							@click="confirm(6)"
						>
							单买(￥{{ singleprice }})
						</text>
						<text
							:class="[
								hasBuy && is_attch_buy && min_quantity > 0 && details.seckill.status == 1 ? 'uni-background-button' : 'disabled uni-button buy',
								details.seckill.use_single == 0 ? 'group' : '',
								'uni-button buy '
							]"
							@click="confirm(7)"
						>
							{{ details.seckill.status == 1 ? '秒杀' : '即将开始' }}
						</text>
					</view>
				</block>
				<block v-if="has_presell">
					<view class="button">
						<!-- <text :class="[hasBuy && is_attch_buy && min_quantity > 0 ? 'uni-background-button1' : 'disabled uni-button', 'uni-button ']" @click="confirm(6)">
							单买(￥{{ singleprice }})
						</text> -->
						<text :class="[hasBuy && is_attch_buy && min_quantity > 0 ? 'uni-background-button' : 'disabled uni-button buy', 'uni-button group ']" @click="confirm(8)">
							立即购买
						</text>
					</view>
				</block>

				<u-gap height="50" v-if="show_empty"></u-gap>
			</view>
		</u-popup>
	</view>
</template>

<script>
import {mapState, mapMutations} from 'vuex';
import util from '@/common/util.js';
export default {
	data() {
		return {
			has_leader: 0,
			success_num: [],
			priceSum: 0,
			singleprice: 0,
			skuTypeList: 0,
			quantity: 0,
			popType: 'bottom',
			count: 1,
			selectimg: '',
			hasBuy: false,
			details: {},
			selectsku: {},
			show: false,
			attachlist: [],
			is_vip: false,
			show_empty: false,
			has_group: false,
			group_level: 0,
			has_seckill: false,
			has_presell: false
			// is_attch_buy:false,
		};
	},
	props: {
		cid: {
			type: Number,
			default: 0
		},
		g_id: {
			type: Number,
			default: 0
		},
		l_id:{
			type: Number,
			default: 0
		}
	},

	computed: {
		min_quantity() {
			let t = 0;
			if (this.attach_quantity != '') {
				t = this.quantity - this.attach_quantity > 0 ? this.attach_quantity : this.quantity;
			} else {
				t = this.quantity;
			}

			return t;
		},
		attach_quantity() {
			let n = '';
			this.attachlist.forEach(a => {
				a.list.forEach(c => {
					if (c.checked) {
						if (n == '') {
							n = c.total;
						} else {
							if (n - c.total > 0) {
								n = c.total;
							}
						}
					}
				});
			});
			return n;
		},
		is_attch_buy() {
			let p = true;
			this.attachlist.forEach(a => {
				if (a.need) {
					let data = a.list.filter(_ => _.checked);
					if (data.length < a.need) {
						p = false;
					}
				}
			});
			return p;
		},
		totalprice() {
			let price = 0;
			this.attachlist.forEach(a => {
				a.list.forEach(c => {
					if (c.checked) {
						price += Number(c.price);
					}
				});
			});
			price = Number(this.priceSum) + Number(price);
			price = price * this.count;
			return price.toFixed(2);
		},
		...mapState({
			UserInfo: state => state.userInfo,
			trolley: state => state.trolley,
			skin: state => state.skin
		})
	},
	watch: {
		'trolley.show': {
			handler: function() {
				if (this.trolley.show) {
					if (this.trolley.type == 2) {
						this.getdetail('/integral/shop/goods/', this.trolley.id);
					} else {
						this.getdetail('/goods/', this.trolley.id);
					}
				}
				//监听标签数量变化
				//this.items = data.content;
			},
			deep: false
		}
	},
	created() {
		// #ifdef MP-WEIXIN
		this.show_empty = util.getShowType();
		// #endif
		// if(this.l_id){
		// 	console.log(this.l_id)
		// 	this.group_level=this.l_id
		// }
		
	},
	methods: {
		selectgroup(i) {
			this.group_level = i;
			if (this.details.activity_prices) {
				this.priceSum = this.details.activity_prices[this.group_level];
			} else {
				this.querySumPrice();
			}
		},
		getdetail(url, id) {
			var params = {};
			this.$http(url + id, params, 'GET').then(res => {
				this.attachlist = [];
				this.is_vip = false;
				let jsondata = res.data;
				let details = jsondata.msg;
				this.details = details;
				this.priceSum = details.sell_price;
				this.singleprice = details.sell_price;
				//存在sku
				if (details.has_sku) {
					if (this.UserInfo.level > 0 && Number(util.getminprice(details.sku, 1)) > 0) {
						let priceSum = util.getminprice(details.sku, 1);
						this.priceSum = priceSum;
						this.singleprice = priceSum;
						this.is_vip = true;
					}
					let c = [];
					if (this.trolley.sku) {
						let sku = details.sku.filter(a => {
							return a.id == this.trolley.sku;
						});
						let skudata = sku[0];
						let l = skudata.pvs.split(',');

						l.forEach(v => {
							let b = v.split(':');
							c.push(b[1]);
						});
					}
					let skuTypeList = JSON.parse(details.sku_value);
					skuTypeList.forEach(a => {
						a.attrValue.forEach(b => {
							if (c && c.findIndex(e => b.id == e) > -1) {
								b.checked = true;
							} else {
								b.checked = false;
							}
						});
					});
					this.hasBuy = false;
					this.selectimg = '';
					this.skuTypeList = skuTypeList;
				} else {
					//单规格商品
					if (details.level_price && Number(util.getprice(details)) > 0 && this.UserInfo.level > 0) {
						this.priceSum = util.getprice(details);
						this.is_vip = true;
					}
					this.skuTypeList = [];
					this.selectsku = {};
					this.quantity = details.total - details.sell;

					if (this.quantity > 0) {
						this.hasBuy = true;
						this.selectimg = '';
					}
				}
				if (details.has_attach) {
					let attach_value = details.attach_value;
					let c = [];
					if (this.trolley.attach) {
						c = this.trolley.attach;
					}
					attach_value.forEach(a => {
						let list = [];
						a.attrValue.forEach(v => {
							let data = {
								id: v[0],
								name: v[1],
								price: v[2],
								total: v[4],
								checked: false
							};
							if (c.findIndex(d => data.id == d) > -1) {
								data.checked = true;
							}
							list.push(data);
						});
						this.attachlist.push({
							name: a.name,
							need: a.need,
							toplimit: a.toplimit,
							list: list
						});
					});
				}
				if (details.group) {
					let limit_type = JSON.parse(details.group.limit_type);
					let limitation = 0;
					if (limit_type[0] == 1) {
						limitation = Number(limit_type[1]);
					} else if (limit_type[0] == 2) {
						limitation = Number(limit_type[1]);
					}
					if (limitation > 0) {
						if (details.is_limitation) {
							if (details.limitation > 0 && limitation < details.limitation) {
								details.limitation = limitation;
							}
						} else {
							details.limitation = limitation;
							details.is_limitation = 1;
						}
					}
					let groupdetail = {
						pricelist: []
					};
					let group = details.group;
					details.time = (new Date(group.end_time) - new Date()) / 1000;
					let sku = details.group.sku;

					if (details.has_sku) {
						details.sku.forEach(a => {
							let i = sku.findIndex(_ => _.sku_id == a.sku_id);
							if (i > -1) {
								a.is_group = 1;
							} else {
								a.is_group = 0;
							}
						});
					}
					this.groupsku = sku;
					if (group.type == 1) {
						if (details.group.has_sku) {
							details.activity_price = this.getminprice(sku, 1);
							details.sku.forEach(a => {
								let i = sku.findIndex(_ => _.sku_id == a.sku_id);
								if (i > -1) {
									a.activity_price = sku[i].activity_price;
									a.activity_stock = Number(sku[i].activity_stock);
									a.leader_price = sku[i].leader_price;
									a.has_group = true;
									a.group_type = 1;
								} else {
									a.has_group = false;
								}
							});
						} else {
							this.quantity = group.activity_stock;

							this.has_leader = !!(Number(this.details.group.leader_price) > -1);
							if (this.has_leader && !this.g_id) {
								this.priceSum = this.details.group.leader_price;
							} else {
								this.priceSum = this.details.group.activity_price;
							}
							this.has_group = true;
						}
					} else {
						this.success_num = JSON.parse(group.success_num);
						if (details.group.has_sku) {
							details.activity_price = this.getminprice(sku, 2);
							console.log(details.activity_price)
							details.sku.forEach(a => {
								let i = sku.findIndex(_ => _.sku_id == a.sku_id);
								if (i > -1) {
									a.activity_price = sku[i].activity_price;
									a.activity_stock = Number(sku[i].activity_stock);
									a.leader_price = sku[i].leader_price;
									a.has_group = true;
									a.group_type = 2;
								} else {
									a.has_group = false;
								}
							});
						} else {
							this.has_group = true;
							let activity_price = JSON.parse(group.activity_price);
							let activity_stock = JSON.parse(group.activity_stock);
							details.activity_price = activity_price[0];
							details.activity_prices = activity_price;
							details.activity_stock = activity_stock;

							this.has_leader = !!(Number(this.details.group.leader_price) > -1);
							if (this.has_leader && !this.g_id) {
								this.priceSum = this.details.group.leader_price;
							} else {
								if(this.g_id){
									this.priceSum = this.details.activity_prices[this.l_id];
								}else{
									this.priceSum = this.details.activity_prices[this.group_level];
								}
								
							}
						}
					}
				} else {
					this.has_group = false;
				}
				if (details.seckill) {
					if (details.seckill.limit_type) {
						let limit_type = JSON.parse(details.seckill.limit_type);
						let limitation = 0;
						if (limit_type[0] == 1) {
							limitation = Number(limit_type[1]);
						} else if (limit_type[0] == 2) {
							limitation = Number(limit_type[1]);
						}
						if (limitation > 0) {
							if (details.is_limitation) {
								if (details.limitation > 0 && limitation < details.limitation) {
									details.limitation = limitation;
								}
							} else {
								details.limitation = limitation;
								details.is_limitation = 1;
							}
						}
					}

					let seckilldetail = {
						pricelist: []
					};
					let seckill = details.seckill;
					if (seckill.status == 0) {
						if (seckill.show_time) {
							if (new Date() - new Date(seckill.show_time) > 0) {
								details.time = (new Date(seckill.start_time) - new Date()) / 1000;
							}
						}
					} else {
						details.time = (new Date(seckill.end_time) - new Date()) / 1000;
					}
					let sku = seckill.sku;
					if (details.has_sku) {
						details.sku.forEach(a => {
							let i = sku.findIndex(_ => _.sku_id == a.sku_id);
							if (i > -1) {
								a.is_seckill = 1;
							} else {
								a.is_seckill = 0;
							}
						});
					}
					if (seckill.has_sku) {
						details.activity_price = this.getminprice(sku, 1);
						details.sku.forEach(a => {
							let i = sku.findIndex(_ => _.sku_id == a.sku_id);
							if (i > -1) {
								a.activity_price = sku[i].activity_price;
								a.activity_stock = Number(sku[i].activity_stock);
								a.has_seckill = true;
							} else {
								a.has_seckill = false;
							}
						});
					} else {
						this.has_seckill = true;
						this.quantity = seckill.activity_stock;
						this.priceSum = seckill.activity_price;
						details.activity_price = seckill.activity_price;
						details.activity_stock = seckill.activity_stock;
					}
				} else {
					this.has_seckill = false;
				}

				if (details.presell) {
					if (details.presell.limit_type) {
						let limit_type = JSON.parse(details.presell.limit_type);
						let limitation = 0;
						if (limit_type[0] == 1) {
							limitation = Number(limit_type[1]);
						} else if (limit_type[0] == 2) {
							limitation = Number(limit_type[1]);
						}
						if (limitation > 0) {
							if (details.is_limitation) {
								if (details.limitation > 0 && limitation < details.limitation) {
									details.limitation = limitation;
								}
							} else {
								details.limitation = limitation;
								details.is_limitation = 1;
							}
						}
					}

					let preselldetail = {
						pricelist: []
					};
					let presell = details.presell;
					if (presell.status == 0) {
						if (presell.show_time) {
							if (new Date() - new Date(presell.show_time) > 0) {
								details.time = (new Date(presell.start_time) - new Date()) / 1000;
							}
						}
					} else {
						details.time = (new Date(presell.end_time) - new Date()) / 1000;
					}
					let sku = presell.sku;
					if (details.has_sku) {
						details.sku.forEach(a => {
							let i = sku.findIndex(_ => _.sku_id == a.sku_id);
							if (i > -1) {
								a.is_presell = 1;
							} else {
								a.is_presell = 0;
							}
						});
					}
					if (presell.has_sku) {
						details.activity_price = this.getminprice(sku, 1);

						details.sku.forEach(a => {
							let i = sku.findIndex(_ => _.sku_id == a.sku_id);
							if (i > -1) {
								a.activity_price = sku[i].activity_price;
								a.activity_stock = Number(sku[i].activity_stock);
								a.has_presell = true;
							} else {
								a.has_presell = false;
							}
						});
					} else {
						this.has_presell = true;
						this.quantity = presell.activity_stock;
						this.priceSum = presell.activity_price;
						details.activity_price = presell.activity_price;
						details.activity_stock = presell.activity_stock;
					}
				} else {
					this.has_presell = false;
				}
				this.$store.commit('settrolley', {show: false, id: 0, type: this.trolley.type});
				this.count = 1;
				this.show = true;

				// this.$refs.popup.open();
			});
		},
		getminprice(list, type) {
			// let minprice=0
			if (type == 1) {
				let min_list = list.sort((a, b) => {
					return Number(a.activity_price) - Number(b.activity_price) > 0;
				});
				return min_list[0].activity_price;
			} else {
				console.log(list);
				list.forEach(a => {
					a.activity_price = JSON.parse(a.activity_price);
					let activity_price = JSON.parse(JSON.stringify(a.activity_price));
					a.minprice = activity_price.sort((d, f) => {
						return Number(d) - Number(f) > 0;
					});
				});
				let min_list = list.sort((a, b) => {
					return Number(a.minprice[0]) - Number(b.minprice[0]) > 0;
				});
				return min_list[0].minprice[0];
			}
		},
		getSku(item, b) {
			if (item.disabled && this.g_id) {
				return false;
			}
			if (item.checked) {
				item.checked = false;
			} else {
				b.attrValue.forEach(a => {
					a.checked = false;
				});
				item.checked = true;
			}
			if (this.details.group) {
				this.limitsku();
			}
			this.querySumPrice();
		},
		limitsku(item, i) {
			let skulist = this.details.sku;
			let pvslist = [];
			this.skuTypeList.forEach(a => {
				a.attrValue.forEach(b => {
					if (b.checked) {
						pvslist.push({
							a_id: a.id,
							b_id: b.id
						});
					}
				});
			});
			for (var i = 0; i < pvslist.length; i++) {
				let pvs = pvslist[i].b_id;
				let mlist = skulist.filter(a => {
					return a.pvs.indexOf(pvs) > -1 && a.is_group;
				});
				this.skuTypeList.forEach((a, index) => {
					if (a.id != pvslist[i].a_id) {
						a.attrValue.forEach(b => {
							let b_id = mlist.findIndex(_ => _.pvs.indexOf(b.id) > -1);

							if (b_id > -1) {
								b.disabled = false;
							} else {
								b.disabled = true;
							}
						});
					} else {
						// a.attrValue.forEach(b => {
						// })
					}
				});
			}
		},
		getattach(c, i) {
			let b = this.attachlist[i];
			let item = this.attachlist[i].list[c];
			if (item.total == 0) {
				return false;
			}
			let data = b.list.filter(c => {
				return c.checked;
			});
			if (data.findIndex(_ => _.id == item.id) > -1) {
				item.checked = false;
				return false;
			}
			let num = data.length;
			if (b.toplimit > 1) {
				if (num < b.toplimit) {
					item.checked = true;
				} else {
					if (item.checked) {
						item.checked = false;
					} else {
						uni.showToast({
							title: '最多选择' + b.toplimit + '项',
							icon: 'none'
						});
					}
				}
			} else {
				b.list.forEach(a => {
					a.checked = false;
				});
				item.checked = true;
			}
		},

		getSelctAttach(type) {
			let l = [];
			this.attachlist.forEach(a => {
				a.list.forEach(b => {
					if (b.checked) {
						if (type == 1) {
							b.pname = a.name;
							l.push(b);
						} else {
							l.push(b.id);
						}
					}
				});
			});
			if (l.length > 0) {
				return l;
			} else {
				return false;
			}
		},
		//返回价格 库存
		querySumPrice() {
			let pvslist = [];
			this.skuTypeList.forEach(a => {
				a.attrValue.forEach(b => {
					if (b.checked) {
						pvslist.push(a.id + ':' + b.id);
					}
				});
			});
			if (pvslist.length == this.skuTypeList.length) {
				let pvs = pvslist.join(',');
				let skulist = this.details.sku;
				skulist.forEach(a => {
					if (a.pvs == pvs) {
						this.quantity = a.total;
						if (a.level_price && this.UserInfo.level > 0) {
							let priceSum = util.getprice(a);
							if (priceSum > 0) {
								this.singleprice = priceSum;
								this.is_vip = true;
							} else {
								this.singleprice = a.sell_price;
								this.is_vip = false;
							}
						} else {
							this.singleprice = a.sell_price;
							this.is_vip = false;
						}
						if (a.has_group) {
							this.quantity = a.activity_stock;
							debugger
							if (Number(a.leader_price) > 0) {
								this.priceSum = a.leader_price;
							} else {
								if (a.group_type == 1) {
									this.priceSum = a.activity_price;
								} else {
									if(this.g_id){
										this.priceSum = a.activity_price[this.l_id];
									}else{
										this.priceSum = a.activity_price[this.group_level];
									}
									
								}
							}

							this.has_group = true;
							this.selectimg = a.pic;
							this.selectsku = a;
							this.hasBuy = a.activity_stock > 0;
							this.count = a.activity_stock > 0 ? 1 : 0;
						} else if (a.has_seckill) {
							this.has_seckill = true;
							this.quantity = a.activity_stock;
							this.priceSum = a.activity_price;
							this.selectimg = a.pic;
							this.selectsku = a;
							this.hasBuy = a.activity_stock > 0;
							this.count = a.activity_stock > 0 ? 1 : 0;
						} else if (a.has_presell) {
							this.has_presell = true;
							this.quantity = a.activity_stock;
							this.priceSum = a.activity_price;
							this.selectimg = a.pic;
							this.selectsku = a;
							this.hasBuy = a.activity_stock > 0;
							this.count = a.activity_stock > 0 ? 1 : 0;
						} else {
							this.has_seckill = false;
							this.has_group = false;
							this.has_presell = false;
							this.quantity = a.total;
							if (a.level_price && this.UserInfo.level > 0) {
								let priceSum = util.getprice(a);
								if (priceSum > 0) {
									this.priceSum = priceSum;
									this.is_vip = true;
								} else {
									this.priceSum = a.sell_price;
									this.is_vip = false;
								}
							} else {
								this.priceSum = a.sell_price;
								this.is_vip = false;
							}

							this.selectimg = a.pic;
							this.selectsku = a;
							this.hasBuy = a.total > 0;
							this.count = a.total > 0 ? 1 : 0;
						}
					}
				});
			} else {
				this.selectsku = {};
				this.hasBuy = 0;
				this.count = 0;
			}
		},
		//确认购买or加入购物车
		confirm(type) {
			if (!this.hasBuy) {
				return false;
			}
			if (type != 3) {
				if (!this.is_attch_buy || this.min_quantity == 0) {
					return false;
				}
			}
			if (type == 5) {
				if (this.details.group.status != 1) {
					return false;
				}
			}
			let _this = this;
			this.judgeLogin(
				() => {
					_this.$store.commit('setlogin', false);
				},
				() => {
					if (!this.hasBuy) {
						return false;
					}
					if (type == 1) {
						let params = {
							commodityId: this.details.id,

							num: this.count
						};
						let attach = this.getSelctAttach();
						if (attach) {
							params.attach = attach;
						}
						if (this.selectsku.sku_id) {
							params.sku = this.selectsku.sku_id;
						}
						if (this.isself) {
							params.type = 2;
							if (this.selectstorestr) {
								params.store_id = this.selectstorestr.id;
							} else {
								uni.showToast({
									title: '请先选择自提门店',
									icon: 'none'
								});
								return false;
							}
						} else {
							params.type = 1;
						}
						this.$http('/trolley', params, 'POST').then(res => {
							if (res.statusCode == 200) {
								uni.showToast({
									title: '加入购物车成功。',
									icon: 'none'
								});
								this.show = false;
								// this.$refs.popup.close();
							} else {
								uni.showToast({
									title: res.data.msg
								});
							}
						});
					} else if (type == 2 || type == 5 || type == 6 || type == 7 || type == 8) {
						let attach = this.getSelctAttach(1);
						let attachval = this.getSelctAttach();
						let data = {
							commodityId: this.details.id,
							sku: this.selectsku.sku_id,
							num: this.count,
							store_id: 1,
							merchantId: this.details.merchant_id,
							master: this.selectimg ? this.selectimg : this.details.master,
							name: this.details.name,
							price: type == 6 ? this.singleprice : this.priceSum,
							pvs_value: this.selectsku.pvs_value,
							type: 1,
							coupon: {},
							deliver: this.details.deliver,
							sendtype: 1,
							attach: attach,
							attachval: attachval,
							is_group: type == 5,
							is_seckill: type == 7,
							is_presell: type == 8,
							has_coupon: 1
						};
						if (this.g_id) {
							data.collage_id = this.g_id;
						} else {
							data.group_level = this.group_level;
						}
						if (data.is_group && type == 5) {
							data.has_coupon = this.details.group.use_coupon;
						}
						if (type == 6) {
							data.has_coupon = 0;
						}
						if (this.selectsku && this.selectsku.has_group && type == 5) {
							data.is_group = this.selectsku.has_group;
						}

						if (this.details.form_open) {
							let form_list = JSON.parse(this.details.form_template.config);
							data.form_list = form_list;
							data.template_id = this.details.form_template.id;
						}

						let deliver = this.details.deliver;
						var sendtype = 'all';
						this.$store.commit('setCommoditys', data);
						this.Jumpurl({
							link: '../order/orderdetail?type=1&sendtype=' + sendtype,
							type: 1,
							login_type: 2
						});
						this.show = false;
					} else if (type == 3) {
						let data = {
							commodityId: this.details.id,
							sku: this.selectsku.id,
							num: this.count,
							store_id: 1,
							merchantId: this.details.merchant_id,
							master: this.selectimg ? this.selectimg : this.details.master,
							name: this.details.name,
							price: this.priceSum,
							pvs_value: this.selectsku.pvs_value,
							type: 1,
							coupon: {}
						};
						this.$store.commit('setCommoditys', data);
						this.Jumpurl({
							link: '/pointsmallpage/order/order?gid=' + this.details.id,
							type: 1,
							login_type: 2
						});
					}
				}
			);
		}
	}
};
</script>

<style lang="scss">
.cart_div {
	padding: 36rpx 36rpx 0 36rpx;
	.head {
		margin-bottom: 20rpx;

		.img_box {
			width: 160rpx;
			height: 160rpx;
			margin-right: 30rpx;
		}

		.detail {
			justify-content: space-between;
			align-items: flex-start;

			.name {
				box-sizing: border-box;
				padding: 0rpx 8rpx;
				width: 480rpx;
			}
			.price {
				padding-left: 8rpx;
				color: #ff5339;
			}
		}
	}

	.color {
		padding: 10rpx 0;

		.title {
			line-height: 2;
			color: #262626;
			font-weight: 700;
		}

		.list {
			flex-wrap: wrap;

			.option {
				border: 1rpx solid #f2f2f2;
				background-color: #f2f2f2;
				line-height: 50rpx;
				padding: 0 40rpx;
				word-wrap: break-word;
				max-width: 100%;
				text-align: left;
				// height: 50rpx;
				color: #262626;
				border-radius: 50rpx;
				margin: 15rpx 25rpx 15rpx 0;
				// display: flex;
				align-items: center;
				&.active {
					color: var(--main-color);
					background-color: var(--content-color);
					border: 1rpx solid var(--main-color);
				}
				&.disabled {
					color: #969696;
				}
				.option-line {
					width: 2rpx;
					height: 24rpx;
					background: rgba(38, 38, 38, 0.2);
					margin: 0 30rpx;
				}
			}
		}
	}

	.num {
		padding: 20rpx 0;

		.tip {
			color: #696969;
		}
	}

	.button {
		margin-top: 10rpx;
		padding: 10rpx;
		margin-left: 45rpx;
		width: 600rpx;
		display: flex;
		.uni-button {
			height: 80rpx;
			width: 300rpx;
			line-height: 80rpx;
			border-radius: 40rpx 0 0 40rpx;
			border: 0;
		}
		.uni-background-button1 {
			background: var(--content-color);
			// color: #000;
			color: var(--main-color);
		}
		.buy {
			border-radius: 0 40rpx 40rpx 0;
		}
		.group {
			width: 600rpx;
			border-radius: 40rpx;
		}
		.uni-button-buy {
			width: 600rpx;
			border-radius: 30rpx;
			border: 0;
		}
	}

	.disabled {
		color: #c9c9c9;
		background-color: #fbfbfb;
		border: 1rpx solid #e6e6e6;
		box-sizing: border-box;
	}
}
</style>
